import os
import sys
import tkinter as tk
from tkinter import messagebox

def check_dependencies():
    """检查必要的依赖是否已安装"""
    required_packages = ['pandas', 'openpyxl', 'numpy']
    missing_packages = []
    
    for package in required_packages:
        try:
            __import__(package)
        except ImportError:
            missing_packages.append(package)
    
    return missing_packages

def install_dependencies(missing_packages):
    """安装缺失的依赖"""
    try:
        import subprocess
        for package in missing_packages:
            print(f"正在安装 {package}...")
            subprocess.check_call([sys.executable, "-m", "pip", "install", package])
        return True
    except Exception as e:
        print(f"安装依赖时出错: {str(e)}")
        return False

def main():
    # 检查依赖
    missing_packages = check_dependencies()
    
    if missing_packages:
        print(f"缺少以下依赖: {', '.join(missing_packages)}")
        print("正在尝试自动安装...")
        
        if not install_dependencies(missing_packages):
            print("自动安装失败，请手动安装以下依赖:")
            print(f"pip install {' '.join(missing_packages)}")
            input("按Enter键退出...")
            return
    
    # 启动GUI程序
    try:
        from excel_comparator_gui import main as start_gui
        start_gui()
    except Exception as e:
        root = tk.Tk()
        root.withdraw()
        messagebox.showerror("错误", f"启动程序时出错: {str(e)}")
        root.destroy()
        print(f"启动程序时出错: {str(e)}")

if __name__ == "__main__":
    main()